package com.sunk.ytmall.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sunk.ytmall.common.Result;
import com.sunk.ytmall.entity.Address;
import com.sunk.ytmall.entity.User;
import com.sunk.ytmall.service.IAddressService;
import com.sunk.ytmall.service.ICommonInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Sunk 673116067@qq.com
 * @Date: 2018/5/9 14:25
 * @Description:关于地址相关api
 */
@RestController
@RequestMapping("/address")
@Transactional
public class AddressController {
    private final Logger logger= LoggerFactory.getLogger(AddressController.class);

    //session中User标识
    private final static String USER="user";

    @Autowired
    private IAddressService addressService;
    @Autowired
    private ICommonInfoService commonInfoService;
    @PostMapping("/addressList")
    public Result getAddressList(){
        Result result=new Result();
        User user=commonInfoService.getUser();
        List<Address> list=addressService.selectList(
                new EntityWrapper<Address>()
                    .eq("user_id", user.getUserId())
                );
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

    @PostMapping("/setDefault")
    public Result setDefault(@RequestBody Address address){
        Result result=new Result();
        User user=commonInfoService.getUser();
        Address userAddress=new Address();
        userAddress.setRecipientsDefault(false);
        addressService.update(userAddress,
                new EntityWrapper<Address>()
                        .eq("user_id", user.getUserId())
        );
        address.setRecipientsDefault(true);
        addressService.updateById(address);
        result.setSuccess(true);
        return result;
    }


    @PostMapping("/delAddress")
    public Result delAddress(@RequestBody Address address){
        Result result=new Result();
        addressService.deleteById(address.getAddressId());
        result.setSuccess(true);
        return result;
    }

    @PostMapping("/addressAdd")
    public Result addressAdd(@RequestBody Address address){
        Result result=new Result();
        User user=commonInfoService.getUser();
        address.setUserId(user.getUserId());
        if(address.getRecipientsDefault()==true){
            Address userAddress=new Address();
            userAddress.setRecipientsDefault(false);
            addressService.update(userAddress,
                    new EntityWrapper<Address>()
                            .eq("user_id", user.getUserId())
            );
        }
        addressService.insert(address);
        return result;
    }

    @PostMapping("/addressUpdate")
    public Result addressUpdate(@RequestBody Address address){
        Result result=new Result();
        Address userAddress=new Address();
        User user=commonInfoService.getUser();
        userAddress.setRecipientsDefault(false);
        addressService.update(userAddress,
                new EntityWrapper<Address>()
                        .eq("user_id", user.getUserId())
        );
        addressService.updateById(address);
        return result;
    }
}
